import { HttpParams } from '@angular/common/http';

export enum GrantType {
    Passowrd = 'password',
    AuthorizationCode = 'authorization_code',
    RefreshToken = 'refresh_token'
}

export enum Scope {
    All = 'all'
}

export const CLIENT_ID = 'test';
export const CLIENT_SECRET = 'test';

export class Oauth2 {
    grantType = GrantType.Passowrd;
    scope = Scope.All;
    clientId = CLIENT_ID;
    clientSecret = CLIENT_SECRET;
    username: string;
    password: string;
    token: string;

    /**
     * putUserNameAndPassword
     */
    public putUserNameAndPassword(username, password) {
        this.username = username;
        this.password = password;
    }

    public getHttpParams(): HttpParams {
        return new HttpParams()
            .set('grant_type', this.grantType)
            .set('scope', this.scope)
            .set('client_id', this.clientId)
            .set('client_secret', this.clientSecret)
            .set('username', this.username)
            .set('password', this.password)
            .set('token', this.token);
    }

    public serialize() {
        return `grant_type=${this.grantType}&scope=${this.scope}&client_id=${this.clientId}&client_secret=${this.clientSecret}`
            + `&username=${this.username}&password=${this.password}`;
    }
}
